class Solution:
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        if "*" not in p and "." not in p:
            flag = self.isSame(s,p)
            return flag
        else:
            flag = self.analysissign(s,p)
            return flag
    
    def isSame(self,s,p):
        flag = False
        if s == p:
            flag = True
        return flag

    def analysissign(self,s,p):
        slist = list(s)
        plist = list(p)
        data = {
            "slist":list(s),
            "plist":list(p),
            "slen":len(slist),
            "plen":len(plist),
            "i":0,
            "j":0
        }
        if len(slist) == 0 or len(plist) == 0:
            flag =False
        else:
            fl = False
            flag = self.judge(data,fl)
        return flag


    def judge(self,data,flag):
        if data["i"] > data["slen"]-1:
            if data["j"] > data["plen"]-1:
                flag = True
            elif data["j"] == "*" and data["j"]==data["plen"]-2:
                flag = True
            return flag
        if data["j"] > data["plen"]-1:
            if data["slen"] == data["plen"]:
                flag = True
            return flag
        if data["plist"][data["j"]] == data["slist"][data["i"]] or data["plist"][data["j"]] == ".":
            data["i"] += 1
            data["j"] += 1
            flag = self.judge(data,flag)
            return flag
        elif data["plist"][data["j"]] == "*":
            if data["plist"][data["j"]-1] == data["slist"][data["i"]] or data["plist"][data["j"]-1]==".":
                data["i"] += 1
                flag = self.judge(data,flag)
            elif data["j"]<data["plen"]-1 and data["plist"][data["j"]+1] == data["slist"][data["i"]]:
                data["j"] += 1
                flag = self.judge(data,flag)
            return flag
        else:
            if data["j"] < data["plen"]-1 and data["plist"][data["j"]+1] == "*":
                data["j"] += 2
                if data["j"]>data["plen"]-1:
                    return flag
                else:
                    flag = self.judge(data,flag)
            return flag


a = Solution()
testlist=[
    # ("aa","a"),
    # ("aa","aa"),
    # ("aaa","aa"),
    ("aa","a*"),
    ("aa",".*"),
    ("ab",".*"),
    ("aab","c*a*b"),
    ("aaa","b*c"),
    ("abcd","d*"),
    ("ab",".*c"),
    ("","..ac"),
    ("aaa",".a"),
    ("a","ab*a")
]
for i in testlist:
    print(a.isMatch(i[0],i[1]))